package top.yh.view;

import top.yh.model.Customer;
import top.yh.service.CustomerList;

import java.util.Scanner;

/**
 * 客户信息显示层
 */
public class CustomerView {
    //初始化容器
    CustomerList customerList = new CustomerList(10);
    //创建扫描仪对象
    Scanner input = new Scanner(System.in);
    //创建主页面方法
    private void mainMenu(){
        while(true){
            //主界面
            System.out.println("---------------客户信息管理系统--------------");
            System.out.println("               1.添加客户");
            System.out.println("               2.修改客户");
            System.out.println("               3.删除客户");
            System.out.println("               4.客户列表");
            System.out.println("               5.退出");
            System.out.println("请选择：[1-5]");
            //接收输入的数
            int number = input.nextInt();
            if(!(number==1 || number==2 || number==3 || number==4 || number==5)){
                System.out.println("您输入有误，请重新输入");
                continue;
            }
            //根据输入的编号选择不同的功能
            mySwitch:switch (number){
                case 1 :
                    //System.out.println("添加客户");
                    addCustomer();
                    break;
                case 2 :
                    System.out.println("修改客户");
                    break;
                case 3 :
                    System.out.println("删除客户");
                    break;
                case 4 :
                    //System.out.println("客户列表");
                    viewCustomerList();
                    break;
                case 5 :
                    while(true){
                        System.out.println("确认是否退出Y/N");
                        String str = input.next();
                        if(str.equalsIgnoreCase("y")){
                            return;
                        }else if(str.equalsIgnoreCase("n")){
                            break mySwitch;
                        }
                    }
            }
        }
    }
    //添加客户
    private void addCustomer() {
        System.out.println("------------------添加客户-----------------");
        System.out.println("姓名");
        String name = input.next();
        System.out.println("姓别");
        String sex = input.next();
        System.out.println("年龄");
        int age = input.nextInt();
        System.out.println("手机号");
        String phone = input.next();
        System.out.println("邮箱");
        String email = input.next();
        //创建用户对象
        Customer customer = new Customer(name,sex,age,phone,email);
        //添加客户
        boolean tag = customerList.add(customer);
        if(tag){
            System.out.println("添加成功");
        }else {
            System.out.println("添加失败");
        }
    }

    //客户列表方法
    private void viewCustomerList() {
        System.out.println("------------------客户列表-----------------");
        //获取客户数据
        Customer[] customerAll = customerList.getCustomersList();
        if(customerAll.length==0){
            System.out.println("没有客户记录");
        }else {
            System.out.println("编号\t姓名\t性别\t年龄\t手机号\t邮箱");
            for(int i=0;i<customerAll.length;i++){
                Customer customer = customerAll[i];
                System.out.println((i+1)+"\t"+customer.getName()+"\t"+customer.getSex()+"\t"+customer.getAge()+"\t"+customer.getPhone()+"\t"+customer.getEmail());
            }
        }
    }

    public static void main(String[] args) {
        //创建对象
        CustomerView customerView = new CustomerView();
        //启动系统
        customerView.mainMenu();
    }
}
